package com.example.security.mapper;

import com.example.security.entity.SysRole;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleMapper {

    @Select("SELECT * FROM sys_role")
    List<SysRole> selectAll();

    @Select("SELECT * FROM sys_role WHERE id = #{id}")
    SysRole selectById(Long id);

    @Select("SELECT * FROM sys_role WHERE role_code = #{roleCode}")
    SysRole selectByCode(String roleCode);

    @Insert("INSERT INTO sys_role(role_name, role_code, description, status, create_time) " +
            "VALUES(#{roleName}, #{roleCode}, #{description}, #{status}, #{createTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(SysRole role);

    @Update("UPDATE sys_role SET role_name=#{roleName}, role_code=#{roleCode}, " +
            "description=#{description}, status=#{status}, update_time=#{updateTime} " +
            "WHERE id=#{id}")
    void update(SysRole role);

    @Delete("DELETE FROM sys_role WHERE id = #{id}")
    void deleteById(Long id);

    @Select("<script>" +
            "SELECT * FROM sys_role " +
            "<where>" +
            "<if test='roleName != null and roleName != \"\"'>" +
            "  AND role_name LIKE CONCAT('%', #{roleName}, '%')" +
            "</if>" +
            "<if test='roleCode != null and roleCode != \"\"'>" +
            "  AND role_code LIKE CONCAT('%', #{roleCode}, '%')" +
            "</if>" +
            "<if test='status != null'>" +
            "  AND status = #{status}" +
            "</if>" +
            "</where>" +
            "ORDER BY create_time DESC" +
            "</script>")
    List<SysRole> selectList();

    @Select("<script>" +
            "SELECT COUNT(*) FROM sys_role " +
            "<where>" +
            "<if test='roleName != null and roleName != \"\"'>" +
            "  AND role_name LIKE CONCAT('%', #{roleName}, '%')" +
            "</if>" +
            "<if test='roleCode != null and roleCode != \"\"'>" +
            "  AND role_code LIKE CONCAT('%', #{roleCode}, '%')" +
            "</if>" +
            "<if test='status != null'>" +
            "  AND status = #{status}" +
            "</if>" +
            "</where>" +
            "</script>")
    long selectCount(SysRole query);

    @Select("<script>" +
            "SELECT * FROM sys_role " +
            "<where>" +
            "<if test='roleName != null and roleName != \"\"'>" +
            "  AND role_name LIKE CONCAT('%', #{roleName}, '%')" +
            "</if>" +
            "<if test='roleCode != null and roleCode != \"\"'>" +
            "  AND role_code LIKE CONCAT('%', #{roleCode}, '%')" +
            "</if>" +
            "<if test='status != null'>" +
            "  AND status = #{status}" +
            "</if>" +
            "</where>" +
            "ORDER BY create_time DESC " +
            "LIMIT #{pageSize} OFFSET #{offset}" +
            "</script>")
    List<SysRole> selectPage(SysRole query);

    List<SysRole> selectByUserId(Long userId);
}